import { Component, OnInit, OnDestroy, ElementRef, Renderer2 } from '@angular/core';
import { FormBuilder, Validators, FormControl } from '@angular/forms';

import { ConfigService } from 'src/app/config.service';
import { AlarmAudioInterface } from './AlarmAudioInterface';
import { IeCssService } from 'src/app/shared/func-service/ie-css.service';
import { ResponseErrorService } from 'src/app/shared/func-service/response-error.service';
import { TipsService } from 'src/app/tips/tips.service';
import { PublicFuncService } from 'src/app/shared/func-service/public-func.service';
import { LayoutService } from 'src/app/shared/func-service/layout.service';
import Logger from 'src/app/logger';


@Component({
  selector: 'app-alarm-audio',
  templateUrl: './alarm-audio.component.html',
  styleUrls: ['./alarm-audio.component.scss']
})
export class AlarmAudioComponent implements OnInit, OnDestroy  {

  constructor(
    private cfg: ConfigService,
    private fb: FormBuilder,
    private Re2: Renderer2,
    private el: ElementRef,
    private ieCss: IeCssService,
    private resError: ResponseErrorService,
    private tips: TipsService,
    private pfs: PublicFuncService,
    private los: LayoutService,
  ) { }

  private logger: Logger = new Logger('alarm-audio');

  isIe: boolean = true;

 
  AlarmAudioForm = this.fb.group({
    sServerUrl: ['', Validators.required],
  });

  get sServerUrl(): FormControl {
    return this.AlarmAudioForm.get('sServerUrl') as FormControl;
  }
  

  ngOnInit() {
    this.isIe = this.ieCss.getIEBool();
    this.cfg.getAlarmAudioInterface().subscribe(
      res => {
        this.resError.analyseRes(res);
        this.AlarmAudioForm.patchValue(res);
      },
      err => {
        this.logger.error(err, 'ngOnInit:getAlarmAudioInterface:');
        this.tips.showInitFail();
      }
    );
  }

  ngOnDestroy() {
   
  }

  onSubmit() {
    this.cfg.setAlarmAudioInterface(this.AlarmAudioForm.value).subscribe(
      res => {
        this.resError.analyseRes(res);
        this.AlarmAudioForm.patchValue(res);
        this.tips.showSaveSuccess();
      },
      err => {
        
        this.logger.error(err, 'onSubmit:setAlarmAudioInterface:');
        this.tips.showSaveFail();
      }
    );
  }

  

  onTest() {
  
  }
}
